package com.xq.flutter_auto_track;

import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.NonNull;

import androidx.annotation.Nullable;

import com.bytedance.ads.convert.BDConvert;
import com.bytedance.applog.IDataObserver;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;

import com.bytedance.applog.AppLog;
import com.bytedance.applog.InitConfig;
import com.bytedance.applog.util.UriConstants;
import org.json.JSONObject;

import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class AutoTrackManager {

    static String deviceBDDID = "";
    static Timer closeAdTimer;
    static Boolean alreadyQueryBDDID = false;

    /// AutoTrack 初始化
    public static void setup(@NonNull Map argument, @NonNull Result result, Context mContext) {

            String appid = (String) argument.get("appid");
            String channel = (String) argument.get("channel");
            boolean needEncrypt = (boolean) argument.get("needEncrypt");
            boolean enabledAutoTrack = (boolean) argument.get("enabledAutoTrack");
            boolean openDebugMode = (boolean) argument.get("openDebugMode");

            final InitConfig config = new InitConfig(appid, channel);
            //设置数据上送地址
            config.setUriConfig(UriConstants.DEFAULT);
            // 加密开关，SDK 5.5.1及以上版本支持，false 为关闭加密，上线前建议设置为 true
            AppLog.setEncryptAndCompress(needEncrypt);
            config.setEnablePlay(true);
            config.setAutoStart(enabledAutoTrack);
            config.setImeiEnable(false);
            config.setAutoTrackEnabled(false);

            //日志开关，debug阶段建议开启
            config.setLogEnable(openDebugMode);
            BDConvert.getInstance().init(mContext, AppLog.getInstance());

            // 联调白名单
            config.enableDeferredALink();
            AppLog.init(mContext, config);
            AppLog.setHeaderInfo("csj_attribution",1);
            result.success(1);
    }

    /// 自定义的上报
    public static void event(@NonNull Map argument, @NonNull Result result) {

            String eventName = (String) argument.get("name");
            JSONObject paramsObj = (JSONObject) argument.get("params");
            AppLog.onEventV3(eventName, paramsObj);
            result.success(1);
    }

    /// 获取BDid
    public static void getTrackInfo(@NonNull Result result, Context mContext) {
        alreadyQueryBDDID = false;
        deviceBDDID = AppLog.getDid();
        // 5秒兜底返回
        closeAdTimer = new Timer();
        closeAdTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                if (!alreadyQueryBDDID) {
                    result.success(deviceBDDID);
                    alreadyQueryBDDID = true;
                }
                closeAdTimer.cancel();
            }
        }, 5000);
        if (!TextUtils.isEmpty(deviceBDDID)) {
            result.success(deviceBDDID);
            alreadyQueryBDDID = true;
        } else {
            AppLog.addDataObserver(new IDataObserver() {
                @Override
                public void onIdLoaded(@NonNull String s, @NonNull String s1, @NonNull String s2) {
                    if (!TextUtils.isEmpty(s)) {
                        alreadyQueryBDDID = true;
                        result.success(s);
                    }
                }

                @Override
                public void onRemoteIdGet(boolean b, @Nullable String s, @NonNull String s1, @NonNull String s2, @NonNull String s3, @NonNull String s4, @NonNull String s5) {
                    String bd_did = AppLog.getDid();
                    if (!TextUtils.isEmpty(bd_did)) {
                        alreadyQueryBDDID = true;
                        result.success(bd_did);
                    }
                }

                @Override
                public void onRemoteConfigGet(boolean b, @Nullable JSONObject jsonObject) {

                }

                @Override
                public void onRemoteAbConfigGet(boolean b, @NonNull JSONObject jsonObject) {

                }

                @Override
                public void onAbVidsChange(@NonNull String s, @NonNull String s1) {

                }
            });
        }
    }
}
